﻿using System;
using System.Globalization;
using System.Linq;
using System.Windows;
using System.Windows.Data;

namespace LpbPrj.Client.Tools.Converter
{
    /// <summary>
    /// 有值或者值为true 显示，否则隐藏；parameter不为null，则逻辑翻转
    /// </summary>
    public class Object2VisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            Visibility output = Visibility.Collapsed;

            if (value == null)
            {
                output = parameter == null ? Visibility.Collapsed : Visibility.Visible;
            }
            else
            {
                output = parameter == null ? Visibility.Visible : Visibility.Collapsed;
            }


            return output;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    /// <summary>
    /// 数值和参数相等 显示，否则隐藏
    /// </summary>
    public class EqualVisibleConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null && parameter != null)
            {
                int data1 = System.Convert.ToInt32(value);
                int data2 = System.Convert.ToInt32(parameter);
                if (data1 == data2)
                {
                    return Visibility.Visible;
                }
            }
            return Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    /// <summary>
    /// 数值和参数相等 隐藏，否则显示
    /// </summary>
    public class EqualCollapsedConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null && parameter != null)
            {
                int data1 = System.Convert.ToInt32(value);
                int data2 = System.Convert.ToInt32(parameter);
                if (data1 == data2)
                {
                    return Visibility.Collapsed;
                }
            }
            return Visibility.Visible;
        }

        public object ConvertBack(object value, Type targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    /// <summary>
    /// 多绑定 有一个为true 则显示，否则隐藏，参数为0时，逻辑翻转
    /// </summary>
    public class MultiOrEqualVisibleConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            Visibility output = Visibility.Collapsed;

            if (values != null && values.Count() > 0)
            {
                {
                    bool param = true;
                    if (parameter != null)
                    {
                        param = System.Convert.ToBoolean(parameter);
                    }
                    var count = values.Where(p => p != DependencyProperty.UnsetValue && p != null && System.Convert.ToBoolean(p)).Count();
                    if (count == 0)
                    {
                        output = param ? Visibility.Collapsed : Visibility.Visible;
                    }
                    else
                    {
                        output = param ? Visibility.Visible : Visibility.Collapsed;
                    }
                }
            }
            return output;
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    /// <summary>
    /// 多绑定 都为true 则显示，否则隐藏，参数为0时，逻辑翻转
    /// </summary>
    public class MultiAndEqualVisibleConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            Visibility output = Visibility.Collapsed;

            if (values != null && values.Count() > 0)
            {
                {
                    bool param = true;
                    if (parameter != null)
                    {
                        param = System.Convert.ToBoolean(parameter);
                    }
                    var count = values.Where(p => p != DependencyProperty.UnsetValue && p != null && System.Convert.ToBoolean(p)).Count();
                    if (count == values.Count())
                    {
                        output = param ? Visibility.Visible : Visibility.Collapsed;
                    }
                    else
                    {
                        output = param ? Visibility.Collapsed : Visibility.Visible;
                    }
                }
            }
            return output;
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
